﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE.DO;
using VINETLIFE.BL;
using System.Data;

namespace VINETLIFE.AdminCP
{
    public partial class ProductCategory : System.Web.UI.Page
    {
        cmsProductCategoryDO objCate = new cmsProductCategoryDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["Pid"]))
            {
                ltrAction.Text = "Cập nhật danh mục sản phẩm";
                int productCategoryID = int.Parse(Request.QueryString["Pid"]);
                bindToDropDown(ddlParentCate);
                objCate.ProductCategoryID = productCategoryID;
                if (!IsPostBack)
                {
                    InitForm();
                    grvPropertyDataSource(productCategoryID);
                }
                divThuocTinh.Visible = false;
                divDanhMuc.Visible = true;
            }
            else
            {
                ltrAction.Text = "Thêm danh mục sản phẩm";
                bindToDropDown(ddlParentCate);
                divDanhMuc.Visible = true;
                divThuocTinh.Visible = false;
            }
        }
        protected void grvPropertyDataSource(int productCategoryID)
        {
            grvProperties.DataSource = new cmsPropertiesBL().SelectByProductCategory(productCategoryID);
            grvProperties.DataBind();
        }
        public void bindToDropDown(DropDownList ddl)
        {
            DataTable dtCate = new cmsProductCategoryBL().SelectAll(); //new DataView(new cmsMenuBL().SelectAll(),"IsContent = 0","",DataViewRowState.CurrentRows).ToTable();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["ProductCategoryID"].ToString();
                    item.Text = "- " + dr["ProductCategoryTitle"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["ProductCategoryID"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + "+ " + dr["ProductCategoryTitle"].ToString();
                    listChild.Value = dr["ProductCategoryID"].ToString();
                    listChild.Attributes.Add("style", "background:#f9f9f9;color:#648293;");
                    ddl.Items.Add(listChild);
                    //       GetChildItems(dr["MenuID"].ToString(), dtTemp, ddl);
                }
            }
        }
        protected void InitForm()
        {
            objCate = new cmsProductCategoryBL().Select(objCate);
            txtContents.Text = objCate.Content;
            txtProductCategoryTitle.Text = objCate.ProductCategoryTitle;
            txtProductCategoryCode.Text = objCate.ProductCategoryCode;
            cbIsHomePage.Checked = objCate.IsHomePage;
            txtDescription.Text = objCate.Description;
            txtOrderID.Text = objCate.OrderID.ToString();
            txtMetaDes.Text = objCate.MetaDes;
            txtMetaTag.Text = objCate.MetaTag;
            txtpagsTitle.Text = objCate.PageTitle;
            try
            {
                ddlParentCate.SelectedValue = objCate.ParentID.ToString();
            }
            catch
            {
            }
        }
        protected void SetObject()
        {
            objCate.ProductCategoryTitle = txtProductCategoryTitle.Text.Trim();
            objCate.ProductCategoryCode = txtProductCategoryCode.Text.Trim();
            objCate.IsHomePage = cbIsHomePage.Checked;
            objCate.Description = txtDescription.Text.Trim();
            objCate.Content = txtContents.Text.Trim();
            objCate.MetaDes = txtMetaDes.Text.Trim();
            objCate.MetaTag = txtMetaTag.Text.Trim();
            objCate.PageTitle = txtpagsTitle.Text.Trim();

            try
            {
                objCate.OrderID = int.Parse(txtOrderID.Text.Trim());
            }
            catch { }
            try
            {
                objCate.ParentID = int.Parse(ddlParentCate.SelectedValue);
            }
            catch { }
        }

        protected void btnLuuDanhMuc_Click(object sender, EventArgs e)
        {
            SetObject();
            cmsProductCategoryDO DO = new cmsProductCategoryDO();
            DO.ProductCategoryCode = txtProductCategoryCode.Text;
            DO = new cmsProductCategoryBL().Selectbycode(DO);


            if (objCate.ProductCategoryID <= 0)
            {
                //if (DO.ProductCategoryID == 0)
                //{
                int pid = new cmsProductCategoryBL().Insert(objCate);
                //if (pid > 0)
                //{
                //Functions.Alert("Thêm danh mục sản phẩm thành công!", "ProductCategory.aspx?Pid=" + pid);
                Response.Redirect("ListProductCategory.aspx");
                //    }
                //    else
                //    {
                //        Functions.Alert("Có lỗi xảy ra!", "ProductCategory.aspx");
                //    }
                //}
                //else
                //{
                //    Functions.Alert("Trùng Mã", "ProductCategory.aspx");
                //}
            }
            else
            {
                //if (DO.ProductCategoryID < 0 || DO.ProductCategoryID == objCate.ProductCategoryID)
                //{
                new cmsProductCategoryBL().Update(objCate);
               // Functions.Alert("Cập nhật danh mục sản phẩm thành công!", "ProductCategory.aspx?Pid=" + objCate.ProductCategoryID.ToString());

                Response.Redirect("ListProductCategory.aspx");
                //}
                //else
                //{
                //    Functions.Alert("Trùng Mã", "ProductCategory.aspx");
                //}
            }
            Ultility.ClearAllCache();
        }

        protected void grvProperties_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvProperties.EditIndex = e.NewEditIndex;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }

        protected void grvProperties_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            cmsPropertiesDO objProperty = new cmsPropertiesDO();
            objProperty.PropertyID = Convert.ToInt32(((Label)grvProperties.Rows[e.RowIndex].Cells[0].FindControl("lblPropertyID")).Text);
            objProperty = new cmsPropertiesBL().Select(objProperty);
            objProperty.PropertyCode = ((TextBox)grvProperties.Rows[e.RowIndex].Cells[1].FindControl("txtPropertyCode")).Text.Trim();
            objProperty.PropertyName = ((TextBox)grvProperties.Rows[e.RowIndex].Cells[2].FindControl("txtPropertyName")).Text.Trim();
            objProperty.PropertyType = ((DropDownList)grvProperties.Rows[e.RowIndex].Cells[3].FindControl("ddlPropertyType")).SelectedValue;
            new cmsPropertiesBL().Update(objProperty);

            grvProperties.EditIndex = -1;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }

        protected void grvProperties_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvProperties.EditIndex = -1;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }
        protected void grvProperties_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("EmptyInsert"))
            {
                TextBox txtProductCode = (TextBox)grvProperties.Controls[0].Controls[0].FindControl("txtPropertyCode");
                TextBox txtProductTitle = (TextBox)grvProperties.Controls[0].Controls[0].FindControl("txtEmptyPropertyName");
                DropDownList ddlProductType = (DropDownList)grvProperties.Controls[0].Controls[0].FindControl("ddlEmpPropertyType");

                cmsPropertiesDO objProperty = new cmsPropertiesDO();
                objProperty.PropertyName = txtProductTitle.Text.Trim();
                objProperty.PropertyCode = txtProductCode.Text.Trim();
                objProperty.PropertyType = ddlProductType.SelectedValue;
                objProperty.ProductCategoryID = int.Parse(Request.QueryString["Pid"]);
                cmsPropertiesDO DO = new cmsPropertiesBL().SelectbyCode(objProperty.PropertyCode);
                if (string.IsNullOrEmpty(DO.PropertyName))
                {
                    new cmsPropertiesBL().Insert(objProperty);
                    Functions.Alert("Thêm thuộc tính thành công!", "ProductCategory.aspx?Pid=" + objCate.ProductCategoryID.ToString());
                    //  grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
                }
                else
                {
                    Functions.Alert("Trùng Mã!", "ProductCategory.aspx?Pid=" + objCate.ProductCategoryID.ToString());
                }
            }
            else if (e.CommandName.Equals("Cancel"))
            {
                grvProperties.ShowFooter = false;
                grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
            }
            if (e.CommandName.Equals("Insert"))
            {

                TextBox txtProductCode = (TextBox)grvProperties.FooterRow.FindControl("txtFPropertyCode");
                TextBox txtProductTitle = (TextBox)grvProperties.FooterRow.FindControl("txtFPropertyName");
                DropDownList ddlProductType = (DropDownList)grvProperties.FooterRow.FindControl("ddlfPropertyType");
                //kiểm tra code
                cmsPropertiesDO DO = new cmsPropertiesBL().SelectbyCode(txtProductCode.Text);
                if (string.IsNullOrEmpty(DO.PropertyName))
                {

                    cmsPropertiesDO objProperty = new cmsPropertiesDO();
                    objProperty.PropertyName = txtProductTitle.Text.Trim();
                    objProperty.PropertyType = ddlProductType.SelectedValue;
                    objProperty.PropertyCode = txtProductCode.Text;
                    objProperty.ProductCategoryID = int.Parse(Request.QueryString["Pid"]);
                    new cmsPropertiesBL().Insert(objProperty);
                    Functions.Alert("Thêm thuộc tính thành công!", "ProductCategory.aspx?Pid=" + objCate.ProductCategoryID.ToString());
                }
                else
                {
                    Functions.Alert("Trùng Mã!", "ProductCategory.aspx?Pid=" + objCate.ProductCategoryID.ToString());
                }
            }
        }
        protected void AddNewRecord(object sender, EventArgs e)
        {
            grvProperties.ShowFooter = true;
            grvPropertyDataSource(int.Parse(Request.QueryString["Pid"]));
        }
        public string ProductCategoryTitle()
        {
            string s = "";
            if (!String.IsNullOrEmpty(Request.QueryString["Pid"]))
            {
                int pid = int.Parse(Request.QueryString["Pid"]);
                s = new cmsProductCategoryBL().Select(new cmsProductCategoryDO { ProductCategoryID = pid }).ProductCategoryTitle;
            }
            return s;
        }

        protected void grvProperties_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            new cmsPropertiesBL().Delete(new cmsPropertiesDO { PropertyID = Convert.ToInt32(grvProperties.DataKeys[e.RowIndex].Value) });
            Functions.Alert("Xóa thuộc tính thành công!", "ProductCategory.aspx?Pid=" + objCate.ProductCategoryID.ToString());
        }
    }
}